package org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.navigation;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.opentcs.kcvehicle.communication.kc.common.byteutils.ByteUtils;

public class Action {
  //动作类型,2个字节
  private byte[] actionType;
  //执行动作并行方式,1个字节
  private byte actionParallel;
  //预留，1个字节
  private byte actionRetain;
  //动作 id,4个字节，actionId 自己定义不要重复就行，可以从0开始累加
  private byte[] actionId;
  //参数长度,1个字节
  private byte paramSize;
  //预留，3个字节
  private byte[] paramRetain2;
  //参数内容,这里的长度 是  paramSize 长度,todo 注意 这里的paramContent 需要字节去构建一下 ，目前 这里还没有
  private byte[] paramContent;


  public Action(
      byte actionType, byte actionParallel, Integer actionId, Integer paramSize, byte[] paramContent
  ) {
    this.actionType = new byte[]{actionType, (byte) 0x00};
    this.actionParallel = actionParallel;
    this.actionRetain = (byte) 0x00;
    this.actionId = ByteUtils.intToBytes(actionId, 1);
    this.paramSize = ByteUtils.usintTo1Byte(paramSize);
    this.paramRetain2 = new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00};
    this.paramContent = paramContent;
  }

  public byte[] toBytes() {
    ByteBuf byteBuf = Unpooled.buffer();
    byteBuf.writeBytes(actionType);
    byteBuf.writeByte(actionParallel);
    byteBuf.writeByte(actionRetain);
    byteBuf.writeBytes(actionId);
    byteBuf.writeByte(paramSize);
    byteBuf.writeBytes(paramRetain2);
    byteBuf.writeBytes(paramContent);
    int i = byteBuf.writerIndex();
    byte[] bytes = new byte[i];
    byteBuf.readBytes(bytes);
    return bytes;
  }
}
